<template>
  <div id="uesrtext">
    <textarea
      :placeholder="currentSessionAdmin? '按 Ctrl + Enter 发送':'点击用户头像开始聊天'"
      v-model="content"
      v-on:keyup="addMessage"
      :disabled="!currentSessionAdmin"
    ></textarea>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'uesrtext',
  data() {
    return {
      content: ''
    }
  },
  methods: {
    addMessage(e) {
      if (e.ctrlKey && e.keyCode === 13 && this.content.length) {

        if (!this.currentSessionAdmin) {
          this.$message.error('发送失败,未选择聊天对象')
          return
        }

        let msgObj = new Object()
        msgObj.to = this.currentSessionAdmin.username
        msgObj.content = this.content
        this.$store.state.stomp.send('/ws/chat', {}, JSON.stringify(msgObj))

        this.$store.commit('addMessage', msgObj);
        this.content = '';
      }
    }
  },
  computed: mapState([
    'currentSessionAdmin'
  ]),
}
</script>

<style lang="scss" scoped>
#uesrtext {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 100%;
  height: 30%;
  border-top: solid 1px #ddd;
  > textarea {
    padding: 10px;
    width: 100%;
    height: 100%;
    border: none;
    outline: none;
    background-color: rgb(250, 250, 250);
  }
}
</style>
